package com.asuna.video_website.controller;

import com.asuna.video_website.service.MediaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = "视频相关接口")
@CrossOrigin
@RestController
public class  MediaController {

    private final MediaService mediaService;
    private final String basePath;

    @Autowired
    public MediaController(MediaService mediaService) {
        this.mediaService = mediaService;
        basePath="/root/Videos/";
        //basePath="D:/Videos/";
    }

    @ApiOperation("视频下载接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", value = "分类", required = true, dataType = "String"),
            @ApiImplicitParam(name = "episode", value = "集数", required = true, dataType = "String")})
    @GetMapping(value = "/video/{sort}/{episode}")
    public void getVideo(@PathVariable("sort") String sort, @PathVariable("episode") String episode, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String videoPath = basePath + sort + "/" + episode;
        mediaService.getVideo(videoPath, request, response);
    }

    @ApiOperation("视频上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", value = "分类", required = true, dataType = "String"),
            @ApiImplicitParam(name = "file", value = "视频文件", required = true, dataType = "__file", paramType = "form")})
    @PostMapping(value = "/upload/{sort}")
    public void uploadVideo(@PathVariable("sort") String sort, MultipartFile file) throws IOException {
        String uploadPath = basePath + sort + "/";
        mediaService.uploadVideo(uploadPath, file);
    }

    @GetMapping("/getAllSortAndEpisode")
    public List<Map<String, Object>> getAllSortAndEpisode(){
        return mediaService.getAllSortName(basePath);
    }
}
